package com.hmsproject.search.managedbean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.hmsproject.common.HMSManagedBeans;
import com.hmsproject.common.HMSUtil;
import com.hmsproject.datastructures.Hotel;
import com.hmsproject.db.HMSDBConnectionFactory;
import com.hmsproject.db.HMSDBType;
import com.hmsproject.db.inmemorydb.DealMap;
import com.hmsproject.search.bean.HotelInfo;
import com.hmsproject.search.bean.SearchBean;

@ManagedBean
@SessionScoped
public class SearchView {
	
	private SearchBean searchBean;
	
	private String selectedHotelId;
	
	private List<HotelInfo> hotels;
	
	@ManagedProperty(value="#{dealView}")
    DealView  dealView;
	
	public List<HotelInfo> getHotels() {
		return hotels;
	}

	public void setHotels(List<HotelInfo> hotels) {
		this.hotels = hotels;
	}

	public SearchBean getSearchBean() {
		return searchBean;
	}

	public void setSearchBean(SearchBean searchBean) {
		this.searchBean = searchBean;
	}

	public DealView getDealView() {
		return dealView;
	}

	public void setDealView(DealView dealView) {
		this.dealView = dealView;
	}

	@PostConstruct
	public void init(){
		this.searchBean = new SearchBean();
	}
	
	public String getCurrentMilliSecd(){
		return new Date(System.currentTimeMillis()).toString();
	}
	
	public String getSelectedHotelId() {
		return selectedHotelId;
	}

	public void setSelectedHotelId(String selectedHotelId) {
		this.selectedHotelId = selectedHotelId;
	}

	
	public void searchValuesFromExternal() {

		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		if (params.containsKey("placeName")) {
			System.out.println( params.get("fromDate") +" "+ params.get("toDate") + " "+params.get("placeName"));
			this.getSearchBean().getPlaceBean()
					.setPlaceName(params.get("placeName"));
			try {
				this.getSearchBean().getPlaceBean()
						.setFromDate(formatter.parse(params.get("fromDate")));
				this.getSearchBean().getPlaceBean()
						.setFromDate(formatter.parse(params.get("toDate")));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	public String search(){
		//this.hotels = HMSDBConnectionFactory.getInstance().getDBConnection().searchHotels(this.searchBean);
		this.searchValuesFromExternal();
		System.out.println("CCALLING SEARCH BEAN  " + this.searchBean);
		this.hotels = HMSDBConnectionFactory.getInstance().getDBConnection().searchHotelsExpedia(this.searchBean);
		System.out.println(this.searchBean.getFilterBean().isDealOnly());
		/*if(this.searchBean.getFilterBean().isDealOnly()){
		  this.hotels = this.hotels.stream().filter(a -> a.hasDeal()).collect(Collectors.toList());
		}*/
		System.out.println(hotels);
		return "/search/search.xhtml?faces-redirect=true";
	}
	
	public String gotoViewDeal(Hotel hotels){
		this.selectedHotelId = hotels.getHotelId();
		this.dealView.setDealId(DealMap.getInstance().getDealForHotel(this.selectedHotelId).getDealId());
		this.dealView.init();
		return "/viewHotel.xhtml?faces-redirect=true";
	}
	
	private Hotel getHotel(String name){
		Hotel hotels = new Hotel(name);
		return hotels;
	}
	
	public String getStar(int star){
		
		StringBuilder startString = new StringBuilder();
		
		for(int i = 0; i < star ; i++){
			startString.append("<i class=\"fa fa-star\"></i>");
		}
		return startString.toString();
		
	}
	
	 public String buyAction(){
		   boolean isUserLoggedIn = HMSManagedBeans.getHMSManagedBeans().isUserLoggedIn();
		   
		   if(!isUserLoggedIn){
			   return HMSManagedBeans.getHMSManagedBeans().redirectToSignPage("/deal/viewHotel.xhtml");
		   }
		   
		   this.dealView.setDealData(HMSUtil.getInstance().buyDeal(this.dealView.getDealData(), HMSManagedBeans.getHMSManagedBeans().getEmailAddress()));
		   return "/deal/buyDeal.xhtml?faces-redirect=true";
	   }
}
